﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xinkui.WebModel.BasicControl;
using CommonFramework.Utility;
using Xinkui.DataAccess.AutoGenerator;
using Xinkui.Common;
using Xinkui.WebModel;
using Xinkui.WebModel.Cache;
using Xinkui.Entity.AutoGenerator;
using Xinkui.WebModel.Helper;

public partial class UserControl_UserCenter_Seller_UC_PostEquipment : AccountControl {
    public int Edit {
        get {
            return WebHelper.GetQueryStringValue<int>("idx", 0);
        }
    }

    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            var equInfo = Singleton<EquipmentInfoMaintainDataAccess>.Instance.EquipmentInfoMaintain_View(Edit);
            this.dropType.DataSource = DictCache.Dict((int)DictTypeEnum.FactoryEquipment).Values;
            this.dropType.DataTextField = "Value";
            this.dropType.DataValueField = "Idx";
            this.dropType.DataBind();

            if (equInfo != null && equInfo.IsDeleted == 0 && equInfo.CompanyID == this.BasePage.CurrentLogin.CompanyID) {
                this.txtBuyTime.Text = equInfo.BuyTime;
                this.txtDescription.Text = equInfo.Desciption;
                this.txtEquipmentName.Text = equInfo.EquipmentName;
                this.txtQuantity.Text = equInfo.Quantity.ToString();
                this.dropType.SelectedValue = equInfo.EquipmentType.ToString();
            }
        }
    }

    protected void btnSubmit_Click(object o, EventArgs e) {
        EquipmentInfoMaintain_Entity entity = new EquipmentInfoMaintain_Entity();
        DateTime n = DateTime.Now;
        entity.CreateDate = n;
        entity.AuditDate = n;
        entity.AuditStatus = (int)AuditStatusEnum.New;
        entity.IsDeleted = 0;
        entity.UpdateDate = n;
        if (Edit > 0) {
            entity = Singleton<EquipmentInfoMaintainDataAccess>.Instance.EquipmentInfoMaintain_View(Edit);
            entity.UpdateDate = DateTime.Now;
            if (entity == null || entity.CompanyID != this.BasePage.CurrentLogin.CompanyID) {
                UIHelper.ShowErrorTips("The equipment infomation has error");
                return;
            }
        }


        entity.BuyTime = this.txtBuyTime.Text;
        entity.CompanyID = this.BasePage.CurrentLogin.CompanyID;
        entity.Desciption = this.txtDescription.Text;
        entity.EquipmentName = this.txtEquipmentName.Text;
        entity.EquipmentType = ConvertHelper.ToType<int>(this.dropType.SelectedValue, 0);
        entity.Quantity = ConvertHelper.ToType<int>(this.txtQuantity.Text, 0);
        if (Edit > 0) {
            Singleton<EquipmentInfoMaintainDataAccess>.Instance.EquipmentInfoMaintain_Update(entity);
        }
        else {
            Singleton<EquipmentInfoMaintainDataAccess>.Instance.EquipmentInfoMaintain_Add(entity);
        }

       // UIHelper.ShowSuucessTips("Equipment Infomation Saved", URLHelper.LinkToEquipment());
    }
}